package demo2;

import org.apache.zookeeper.ZooKeeper;

import java.util.ArrayList;
import java.util.List;

public class DisClient {
    String url="192.168.184.100:2181,192.168.184.101:2181";
    int timeout=20000;
    ZooKeeper client;
    void getConnect()throws Exception{
        client= new ZooKeeper(url, timeout, watchedEvent -> {
            try{
                getChildren();
            }catch (Exception e){
                e.printStackTrace();
            }
        });
    }


    public void getChildren() throws Exception {
        List<String>children=client.getChildren("/servers",true);
        List<String>hosts=new ArrayList<>();
        for(String c:children){
            byte[]data=client.getData("/servers/"+c,false,null);
            hosts.add(new String(data));
        }
        System.out.println(hosts);
    }

    public static void main(String[] args) throws Exception {
        DisClient c=new DisClient();
        c.getConnect();
        Thread.sleep(Long.MAX_VALUE);

    }
}
